#include "CEnemy.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../Managers/InputManager.h"
#include "../Core/MessageSystem.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Game States/CGameplayState.h"
#include "CMasterHand.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../Visual/VIN_BitMapFont.h"
#include "CRock.h"

CBaseEnemy::CBaseEnemy()
{
	m_isHeld = false;
	m_bAlive = true;
	m_bPassive = true;
	m_bFriendly = false;
	hasDied = false;
	m_nWeight = 10;

	m_vDistanceToPlayer.fX = 0.0f;
	m_vDistanceToPlayer.fY = 0.0f;
	m_nAttackRange = 0.0f;
	flip = false;
	SetType(OBJ_ENEMY);

	baseHP = 0.0f;

	shadowID = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_SHADOW);
	shadowScale = 2.0f;
	shadowYOffset = 0;
	shadowXOffset = 0;


	speed = 1.0f;
}

CBaseEnemy::~CBaseEnemy()
{
}

void CBaseEnemy::Render()
{
	CSGD_TextureManager* pTM  = CSGD_TextureManager::GetInstance();
	CSGD_Direct3D* d3d  = CSGD_Direct3D::GetInstance();
//	RECT tempRECT;

	RECT poisonrect;
	SetRect(&poisonrect, (int)(GetPosX() - 50), (int)(GetPosY() - 40), (int)(GetPosX() + 50), (int)(GetPosY() + 40));

	//d3d->DrawRect(poisonrect, 0, 255, 0);

//	SetRect(&tempRECT, (int)GetPosX() + GetWidth(), (int)GetPosY() + GetHeight(), (int)GetPosX() + 64, (int)GetPosY() + 128);

//	d3d->DrawRect(tempRECT, 255, 0, 0);

	//shadowScale; // -= ((400+GetGroundPt()) - (GetPosY() + GetHeight())*0.001f);

	if(!hasDied)
		pTM->Draw(shadowID, (int)GetPosX() + shadowXOffset, 400 + GetGroundPt() + shadowYOffset, shadowScale, shadowScale, 0, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB( 255, 0, 0, 0));

//	d3d->GetSprite()->Flush();
//	if(!m_bPassive)
	//	VIN_BitMapFont::GetInstance()->Render("ATTACKED", (int)GetPosX(), (int)GetPosY(),1, D3DCOLOR_ARGB(0, 255, 255, 255), 1);
}

void CBaseEnemy::Update( float fElapsedTime)
{
	if(hasDied)
		return;

	if(GetHP() <= 0)
	{
		MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 0));
		hasDied = true;
		CGamePlayState::GetInstance()->SetDeadEnemies(CGamePlayState::GetInstance()->GetDeadEnemies() + 1);
		MessageSystem::GetInstance()->SendNewMessage(new CCreateMoneyBagMessage(GetPosX(), GetPosY(), 150.0f, 150.0f, 2));
		MessageSystem::GetInstance()->SendNewMessage(new CCreateMoneyBagMessage(GetPosX(), GetPosY(), 150.0f, -150.0f, 2));
		MessageSystem::GetInstance()->SendNewMessage(new CCreateMoneyBagMessage(GetPosX(), GetPosY(), -150.0f, 150.0f, 2));
		MessageSystem::GetInstance()->SendNewMessage(new CCreateMoneyBagMessage(GetPosX(), GetPosY(), -150.0f, -150.0f, 2));
		CGamePlayState::GetInstance()->GetHand()->GainXP(GetBaseHP() * .50f + 2.5f * (float)CGamePlayState::GetInstance()->GetHand()->GetLevel());
	}

	m_vDistanceToPlayer.fX = GetPosX() - CGamePlayState::GetInstance()->GetPlayer()->GetPosX();
	m_vDistanceToPlayer.fY = GetPosY() - CGamePlayState::GetInstance()->GetPlayer()->GetPosY();
	
	if(color == D3DCOLOR_XRGB(255,0,0))
	{
		//MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(GetPosX() + 12,GetPosY() - 16, 0, 0, 0));

		static float colortime = 0.0f;
		colortime += fElapsedTime;

		if(colortime >= 0.75f)
		{
			colortime = 0.0f;
			color = -1;
		}
	}

	if(CGamePlayState::GetInstance()->GetHand()->GetPoison())
	{
		if( CGamePlayState::GetInstance()->GetHand()->GetPosX() > GetPosX() - 50 &&
			CGamePlayState::GetInstance()->GetHand()->GetPosX() < GetPosX() + 50 &&
			CGamePlayState::GetInstance()->GetHand()->GetPosY() > GetPosY() - 40 &&
			CGamePlayState::GetInstance()->GetHand()->GetPosY() < GetPosY() + 40)
		{
			hurtcolor();
			SetHP(GetHP() - (.025f + 0.1f*CGamePlayState::GetInstance()->GetHand()->GetLevel()));
			CGamePlayState::GetInstance()->GetHand()->GainXP(.08f);
			VIN_SoundManager::GetInstance()->PlayHurt();
		}
		else
		{
			color = -1;
		}
	}
	
	if(GetLanded())
	{
		//if(GetPosX() > CGamePlayState::GetInstance()->GetPlayer()->GetPosX())
		{
			//	SetVelX(-50.0f);
		}
		//else
		{
			//	SetVelX(50.0f);

		}
	}
	/*if(GetPosY() > CGamePlayState::GetInstance()->GetPlayer()->GetPosY())
	{
	SetVelY(-10.0f);
	}
	else
	{
	SetVelY(10.0f);
	}*/

	float test = GetPosX();
	float test2 = GetVelX();

	if(!CGamePlayState::GetInstance()->GetHand()->GetHoldingPinch())
	{
		m_isHeld = false;
	}

	if(m_isHeld)
	{
		SetLanded(false);
		SetPosX((float)InputManager::GetInstance()->CursorPosition().x);
		SetPosY((float)InputManager::GetInstance()->CursorPosition().y + 64.0f);
		SetVelX((float)InputManager::GetInstance()->GetCurrentVelocity().x);
		SetVelY((float)InputManager::GetInstance()->GetCurrentVelocity().y);
		//SetVelX(0.0f);
	}
	else
	{
		CBaseObject::Update(fElapsedTime);
	}

	if(!m_bFriendly)
	{
		if(GetPosX() < -100 || GetPosX() > CGamePlayState::GetInstance()->GetPlayer()->GetPosX())
		{
			if(GetPosX() < -100)
				SetPosX(-99.0f);
			else if(GetPosX() > CGamePlayState::GetInstance()->GetPlayer()->GetPosX())
				SetPosX(CGamePlayState::GetInstance()->GetPlayer()->GetPosX() - 1);
			
			//SetVelX(-1.0f*GetVelX());
		}
		if(GetPosY() < -100)
		{
			SetPosY(-99.0f);

			SetVelY(-1.0f*GetVelY());
		}
	}

}
void CBaseEnemy::HandleEvent(CEvent* pEvent)
{
}
bool CBaseEnemy::CheckCollision(IBaseInterface* pBase)
{
	if(hasDied)
		return false;

	if(pBase->GetType() == OBJ_ROCK)
	{
		CRock* rock = (CRock*)pBase;
		RECT temp;
		if(IntersectRect(&temp,&GetHealthRect(),&rock->GetHealthRect()))
		{
			if(rock->GetVelY() > 0)
				SetHP(0);
			else
				SetVelX(0);
			return true;
		}
	}
	else if( pBase->GetType() == OBJ_HAND)
	{
		CMasterHand* hand = (CMasterHand*)pBase;
		RECT temp;
		if(IntersectRect(&temp, &GetHealthRect(), &hand->GetCollisionRect()))
		{
			if(!CGamePlayState::GetInstance()->GetHand()->GetHoldingEnemy())
			{
				if( GetWeight() < 10 + 15 * CGamePlayState::GetInstance()->GetHand()->GetLevel() && GetFriendly() == false)
				{
					//MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(GetPosX() + 12, GetPosY() - 16, 0, 0, 0));
					if(!hand->GetPoison())
					{
						SetHP(GetHP() - 2.0f*(float)CGamePlayState::GetInstance()->GetHand()->GetLevel());
						CGamePlayState::GetInstance()->GetHand()->SetHoldingEnemy(true);
						SetLanded(false);
						m_isHeld = true;
						return true;
					}
				}
			}
		}
	}

	else if( pBase->GetType() == OBJ_ENEMY)
	{
		CBaseEnemy* Enemy = (CBaseEnemy*)pBase;
		RECT temp;
		if(IntersectRect(&temp, &GetHealthRect(), &Enemy->GetHealthRect()))
		{
			if(this->GetLanded() == false && Enemy->GetLanded() == false && GetDistanceToPlayer().GetX() != 0)
			{
				HurtMe(500);
			}
		}
	}
	return false;
}

void CBaseEnemy::HurtMe(int AmountOfDamageTaken)
{
	SetHP(GetHP() - AmountOfDamageTaken);

	// TODO Stop ALL animation and play the hurt animation and hurt sound
}

void CBaseEnemy::Attack()
{
	CSGD_Direct3D* d3d  = CSGD_Direct3D::GetInstance();
	static float fCooldown = 0.0f;
	// Render Attack Animation after cooldown

	m_bPassive = true;
	if(fCooldown >= 3.0f)
	{
		m_bPassive = false;
		return;
		//fCooldown = 0.0f;
	}

	fCooldown += 0.01f;

}

RECT CBaseEnemy::GetHealthRect()
{
	RECT temp = {0,0,0,0};
	return temp;
}